package com.atguigu.commonutils;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class R<T> {

    @ApiModelProperty(value = "是否返回成功")
    private Boolean success;
    @ApiModelProperty(value = "返回码")
    private Integer code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private  T data;
//    private Map<String, T> data =  new HashMap<>();


    private  R() {

    }

    protected static <T>  R<T> build(R result , T data){

        if (data != null){
            result.setData(data);

            return  result;

        }else {
            R<T> results = new R<>();

            return results;
        }

    }

    //成功的静态方法
    public static <T> R<T> ok(){
        R<T> r = build(null,null);
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("成功");

        return  r;
    }

    public static <T>  R<T> data(T data){

        R<T> build = build(ok(), data);

//        return build(data);
        return build;
    }

    //目前在声明为静态的返回类型才可以直接转换Map<String, Object> 为T data类型；移除 static <T> build()生名后失效
    public static <T> R<Map<String,Object>> data(String key, T data){
        Map<String, Object> map =  new  HashMap<>();
        map.put(key,data);
        R<Map<String, Object>> build = build(ok(),map);

        return  build;
    }

    public  static <T>  R<T> datas(String key, T value){
//        R<Map<String, Object>> build = build(ok(),map);
//        return build(ok(), map);

        Map<String, T> map = new HashMap<>();
        map.put(key,value);
//        this.data = (T)map;

//        R<T> objectR = (R<T>) ok().setDatas(key, data);



        return  ok();
    }


    //失败静态方法
    public static <T> R<T> error(){

        R<T> r = new R<>();
        r.setSuccess(false);
        r.setCode(ResultCode.ERROR);
        r.setMessage("失败");

        return  r;
    }

    public R<T> success(Boolean success){
        this.success = success;
        return  this;
    }

    public R<T> code(Integer code){
        this.code = code;
        return  this;
    }

    public R<T> message(String message){
        this.message = message;

        return  this;
    }

    //    data添加map数据
//    public static <T>  R<T> build( T body){
//        R<T> result = new R<>();
//
//        result.setData(body);
//
//
//        return result;
//    }





////    data添加map数据
//    public R<T> data(String key, T value){
//        this.data.put(key,value);
//
//
//        return this;
//    }
//
//
//    public R<T> data(Map<String, T> map){
//
//        if (map != null){
//            this.setData(map);
//        }
//
//        return  this;
//    }
//
//
//
//    public R<T> data(String key, List<Object> list){
//        HashMap<String, Object> tempMap = new HashMap<>();
//        tempMap.put(key, list);
//
//
//        return  this;
//    }


    //    data添加map数据
    public R<T> setDatas(String key, T value){
        Map<String, T> map = new HashMap<>();
        map.put(key,value);

        this.data = (T)map;

        return this;
    }

    //根据标志位是否成功返回R.OK() R.ERROR
    public static <T> R<T> flagCheck(boolean flag){

        if (flag){
            return  R.ok();
        }else{
            return R.error();
        }
    }



    @Override
    public String toString() {
        return "R{" +
                "success=" + success +
                ", code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }


}
